package com.example.final_project.service.impl;

import com.example.final_project.entity.Publisher;
import com.example.final_project.mapper.PublisherMapper;
import com.example.final_project.service.PublisherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PublisherServiceImpl implements PublisherService {

    @Autowired
    private PublisherMapper publisherMapper;

    @Override
    @Transactional
    public boolean add(Publisher publisher) {
        return publisherMapper.insert(publisher) > 0;
    }

    @Override
    @Transactional
    public boolean update(Publisher publisher) {
        return publisherMapper.update(publisher) > 0;
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        return publisherMapper.deleteById(id) > 0;
    }

    @Override
    public Publisher getById(Long id) {
        return publisherMapper.selectById(id);
    }

    @Override
    public PageInfo<Publisher> getList(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Publisher> list = publisherMapper.selectList(name);
        return new PageInfo<>(list);
    }

    @Override
    public List<Publisher> getAll() {
        return publisherMapper.selectList(null);
    }

    @Override
    public int countAllPublishers() {
        return publisherMapper.countAllPublishers();
    }

    @Override
    public List<Publisher> getLatestPublishers(int limit) {
        return publisherMapper.getLatestPublishers(limit);
    }
} 